package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

import java.util.Collections;

/**
 * @author ：乔帅阳
 * @date ：Created in 2021/12/18 21:02
 * @modified By： 最新版本解决跨域问题
 */


@Configuration
public class GulimallCorsConfiguration {


      @Bean
      public CorsWebFilter corsWebFilter(){

          UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
          CorsConfiguration corsConfiguration = new CorsConfiguration();

          //1、配置跨域
          corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
          corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
         /* corsConfiguration.addAllowedOrigin("*");*/
          corsConfiguration.setAllowCredentials(true);
          corsConfiguration.setAllowedOriginPatterns(Collections.singletonList(CorsConfiguration.ALL));
          source.registerCorsConfiguration("/**",corsConfiguration);
          return new CorsWebFilter(source);
      }

}
